package com.sensologyhome_all.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.sensologyhome_all.R;

import java.net.URL;
import java.net.URLConnection;

/**
 * 网络相关工具类
 * @author joyskim
 *
 */
public class NetUtil {

	/**
	 * 判断当前网络是否可用
	 * @return
	 */
	public static boolean checkNetworkIfAvailable(Context context){
		if (context==null)return false;
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivityManager == null){
			return false;
		}else {
			NetworkInfo netWorkInfo = connectivityManager.getActiveNetworkInfo();
			return (netWorkInfo!=null&&netWorkInfo.isAvailable());
		}
	}

	/**
	 * 网络连接状态下判断连接的网络类型
	 * @return
	 */
	public static int getNetWorkType(Context context){
		if(checkNetworkIfAvailable(context)){
			int type=((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo().getType();
			return type== ConnectivityManager.TYPE_WIFI? ConstUtil.NETWORK_TYPE_WIFI: ConstUtil.NETWORK_TYPE_MOBILE;
		}else{
			return ConstUtil.NETWORK_TYPE_DISCONNECTED;
		}
	}

	/**
	 * 打开手机系统的设置页面
	 * @param activity
	 */
	public static void openSystemSettings(Activity activity){
		Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
		// Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
		activity.startActivity(intent);
		activity.overridePendingTransition(R.anim.start_enter, R.anim.start_exit);
	}

	/**
	 *判断GPS是否打开
	 * @param context
	 * @return
	 */
	public static boolean isGpsEnabled(Context context) {
		LocationManager locationManager = ((LocationManager) context.getSystemService(Context.LOCATION_SERVICE));
		return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

	}  
	
	/**
	 * GPS设置
	 * @param activity
	 */
	public static void setGps(Activity activity){
		Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		activity.startActivity(intent);
		activity.overridePendingTransition(R.anim.start_enter, R.anim.start_exit);
	}

	public static long getNetTimeStamp () {
	    try {
	    		URL url=new URL("http://www.bjtime.cn");//取得资源对象
			   URLConnection uc=url.openConnection();//生成连接对象
			   uc.connect(); //发出连接
			   long ld=uc.getDate(); //取得网站日期时间
			   return ld;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		} 
	 }

}
